﻿/*
 * Almathy Center - AutoCompleteAccount.cs
 * Copyright (C) 2011 Christophe LEMOINE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using agsXMPP;
using System.Reflection;
using System.IO;
using System.Xml.Serialization;

namespace AlmathyCenter.Datas
{

    public class AutoCompleteAccountsManager
    {

        public List<AutoCompleteAccount> Accounts = new List<AutoCompleteAccount>();

        private string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar.ToString() + "Accounts" + Path.DirectorySeparatorChar.ToString();

        public AutoCompleteAccountsManager()
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            string[] files = Directory.GetFiles(path, "*.account");
            List<string> Deleting = new List<string>();
            foreach (string file in files)
            {
                try
                {
                    XmlSerializer xs = new XmlSerializer(typeof(AutoCompleteAccount));
                    FileStream fs = new FileStream(file, FileMode.Open);
                    AutoCompleteAccount aca = xs.Deserialize(fs) as AutoCompleteAccount;
                    fs.Close();
                    fs.Dispose();
                    if (!Accounts.Contains(aca))
                        Accounts.Add(aca);

                }
                catch
                {
                    Deleting.Add(file);
                }
            }
            foreach (string file in Deleting)
            {
                try
                {
                    File.Delete(file);
                }
                catch { }
            }
        }

        public void Save()
        {
            List<AutoCompleteAccount> Deleting = new List<AutoCompleteAccount>();
            foreach (AutoCompleteAccount aca in Accounts)
            {
                if (aca.Username != string.Empty && aca.Host != string.Empty)
                {
                    try
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(AutoCompleteAccount));
                        FileStream fs = new FileStream(path + aca.Jid.Bare.ToString() + ".account", FileMode.Create);
                        xs.Serialize(fs, aca);
                        fs.Close();
                        fs.Dispose();
                    }
                    catch { }
                }
                else
                {
                    Deleting.Add(aca);
                }
            }
            foreach (AutoCompleteAccount acab in Deleting)
                Accounts.Remove(acab);

        }

        public bool Add(AutoCompleteAccount aca)
        {
            if (Accounts.Contains(aca))
                return false;

            if (aca.Username != string.Empty && aca.Host != string.Empty)
            {
                try
                {
                    XmlSerializer xs = new XmlSerializer(typeof(AutoCompleteAccount));
                    FileStream fs = new FileStream(path + aca.Jid.Bare.ToString() + ".account", FileMode.Create);
                    xs.Serialize(fs, aca);
                    fs.Close();
                    fs.Dispose();
                    Accounts.Add(aca);
                }
                catch { return false; }

            }
            else
                return false;

            return true;
        }

        public bool Remove(AutoCompleteAccount aca)
        {
            if (!Accounts.Contains(aca))
                return false;

            if (File.Exists(path + aca.Jid.Bare.ToString() + ".account"))
                File.Delete(path + aca.Jid.Bare.ToString() + ".account");

            Accounts.Remove(aca);
            return true;
        }

        public bool ContainsUsername(string username)
        {
            bool result = false;
            foreach (AutoCompleteAccount aca in Accounts)
            {
                if (aca.Username.Trim().ToLower() == username.Trim().ToLower())
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        public AutoCompleteAccount GetAccountByUsername(string username)
        {
            AutoCompleteAccount result = null;
            foreach (AutoCompleteAccount aca in Accounts)
            {
                if (aca.Username.Trim().ToLower() == username.Trim().ToLower())
                {
                    result = aca;
                    break;
                }
            }
            return result;
        }

    }

    [Serializable()]
    public class AutoCompleteAccount
    {

        private bool _AutoIdle = true;
        public bool AutoIdle
        {
            get { return _AutoIdle; }
            set { _AutoIdle = value; }
        }

        private int _AutoIdleDelay = 300;
        public int AutoIdleDelay
        {
            get { return _AutoIdleDelay; }
            set { _AutoIdleDelay = (value >= 30 && value <= 3600) ? value : 300; }
        }

        private int _Status = 0;
        public int Status
        {
            get { return _Status; }
            set { _Status = (value >= 0 && value <= 3) ? value : 0; }
        }

        private int _Activity = 0;
        public int Activity
        {
            get { return _Activity; }
            set { _Activity = (value >= 0 && value <= 7) ? value : 0; }
        }

        private int _Mood = 0;
        public int Mood
        {
            get { return _Mood; }
            set { _Mood = (value >= 0 && value <= 10) ? value : 0; }
        }

        private string _StatusMessage = string.Empty;
        public string StatusMessage
        {
            get { return _StatusMessage; }
            set { _StatusMessage = value.Trim(); }
        }

        private string _Username = string.Empty;
        public string Username
        {
            get { return _Username; }
            set { _Username = value.Trim(); }
        }

        private string _Server = string.Empty;
        public string Server
        {
            get { return _Server; }
            set { _Server = value.Trim(); }
        }

        private string _Host = string.Empty;
        public string Host
        {
            get { return _Host; }
            set { _Host = value.Trim(); }
        }

        private int _Port = 5222;
        public int Port
        {
            get { return _Port; }
            set { _Port = (value >= 0 || value <= 65535) ? value : 5222; }
        }

        private bool _UseCompression = false;
        public bool UseCompression
        {
            get { return _UseCompression; }
            set { _UseCompression = value; }
        }

        private bool _UseSSL = false;
        public bool UseSSL
        {
            get { return _UseSSL; }
            set { _UseSSL = value; }
        }

        [XmlIgnore()]
        public Jid Jid
        {
            get { return new Jid(Username, Host, Assembly.GetExecutingAssembly().GetName().Name); }
        }

        public override string ToString()
        {
            return Username;
        }

    }

}
